package org.artifact.core.util;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.CharsetUtil;

import java.io.UnsupportedEncodingException;

public class CommonUtil {
    public static String getIndex(String prefix, String tag, Object... parameter) {
        return prefix + tag + ArrayUtil.join(parameter, "|");
    }


    public static byte[] toBytes(Object obj) {
        byte[] bytes = null;
        if (obj instanceof String) {
            String v = (String) obj;
            try {
                bytes = v.getBytes(CharsetUtil.UTF_8);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        } else if (obj instanceof Integer) {
            int v = (int) obj;
            bytes = new byte[4];
            BitUtil.putInt(bytes, 0, v);
        } else if (obj instanceof Long) {
            long v = (long) obj;
            bytes = new byte[8];
            BitUtil.putLong(bytes, 0, v);
        }
        return bytes;
    }
}
